package com.guanghua.brick.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class XmlUtil {
	
	public static String toXml(Object o) {
		return null;
	}
	
	public static String objectToXml(Object o) {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("<objects>\n");
		
		sb.append("</objects>\n");
		return sb.toString();
	}
	
	public static String objectArrayToXml(Object[] o) {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("<objects>\n");
		
		sb.append("</objects>\n");
		return sb.toString();
	}
	
	public static String listToXml(List l) {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("<objects>\n");
		
		sb.append("</objects>\n");
		return sb.toString();
	}
	
	public static String mapToXml(Map m) {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("<objects>\n");
		Iterator j = m.keySet().iterator();
		sb.append("	<object>\n");
		while (j.hasNext()) {
			Object key = j.next();
			sb.append("		<property name=\"")
			.append(key)
			.append("\">")
			.append(m.get(key))
			.append("</property>\n");
		}
		sb.append("	</object>\n");
		sb.append("</objects>\n");
		return sb.toString();
	}
	
	public static String mapListToXml(List<?> l) {
		Map[] m = (Map[])l.toArray(new Map[0]);
		return mapArrayToXml(m);
	}
	
	public static String mapArrayToXml(Map[] m) {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("<objects>\n");
		for (int i = 0; m != null && i < m.length; i++) {
			Iterator j = m[i].keySet().iterator();
			sb.append("	<object>\n");
			while (j.hasNext()) {
				Object key = j.next();
				sb.append("		<property name=\"")
				.append(key)
				.append("\">")
				.append(m[i].get(key))
				.append("</property>\n");
			}
			sb.append("	</object>\n");
		}
		sb.append("</objects>\n");
		return sb.toString();
	}
	
	public static String stringToXml(String s) {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("<string>\n");
		if (s != null) {
			sb.append("<value><![CDATA[")
				.append(s)
				.append("]]></value>\n");
		}
		sb.append("</string>\n");
		return sb.toString();
	}
	
	public static String stringArrayToXml(String[] s) {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("<array>\n");
		for (int i = 0; s != null && i < s.length; i ++) {
			sb.append("<value><![CDATA[")
			.append(s[i])
			.append("]]></value>\n");
		}
		sb.append("</array>\n");
		return sb.toString();
	}	
	
	public static String stringListToXml(List<?> l) {
		String[] s = (String[])l.toArray(new String[0]);
		return stringArrayToXml(s);
	}
}
